﻿using HBase;
using hCommand;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Controller.i18n.entity {
    [Serializable]
    public class TaskCrane:BaseTask {
        public const int CENTER_DISTANCE = 6150;
        public static int getBaseDistance(Edge e) {
            //反射使用
            return e.b;
        }
        public int step {//1上缓存 2，下缓存
            get; set;
        }
        public int bussType {
            get; set;
        }
        public byte forkNo {
            get; set;
        }
        public byte craneId {
            get; set;
        }

        public int sx {
            get; set;
        }
        public int sy {
            get; set;
        }
        public int sz {
            get; set;
        }

        public int dx {
            get; set;
        }
        public int dy {
            get; set;
        }
        public int dz {
            get; set;
        }
        public TaskDir getStepDir(int cx) { //任务主执行方向
            if (step == 1) {
                return getStep1Dir();
            } else if (step == 2) {
                return getStep2Dir(cx);
            }

            throw new Exception("任务不完整");
        }
        public TaskDir getStep1Dir() { //任务主执行方向
            List<TaskCrane> list = MyCache.getList<TaskCrane>(typeof(TaskCrane));
            foreach (var tc in list) {
                if (tc.taskNo == taskNo && tc.src == des) { 
                    return changeDir(sx,tc.dx); 
                }
            }
            throw new Exception("任务不完整");
        }

        public TaskDir changeDir(int from,int to) {

            if (from < to) {
                return TaskDir.前进;
            } else if (from > to) {
                return TaskDir.后退;
            } else {
                return TaskDir.原位;
            }

        }
        public TaskDir getStep2Dir(int cx) { //任务主执行方向
            return changeDir(cx,dx);

        }

        public override bool canWork(long meId,long beforeId,List<long> relyTask) {

            BaseTask me = PathFacade.getTaskById(meId);
            BaseTask b = PathFacade.getTaskById(beforeId);
            if (b == null) {
                return true;
            }
            CoreTaskParam p = CoreTaskParam.getCoreTaskParam(me.completeParamId);
            CoreTaskParam bp = CoreTaskParam.getCoreTaskParam(b.completeParamId);

            if (p.des == bp.src) {//避让搬运 
                if (b.status < (int)CoreTaskParamStatus.取货完成) {
                    LoggerCommon.consoleOnly(0,"依赖垛机{0}{1}{2}",me.taskNo,b.className,b.taskNo);
                    return false;
                }
                return true;
            }
            if (p.src == bp.des) {//避让搬运 
                return bp.status > (int)CoreTaskParamStatus.待执行;

            }

            return true;

        }
        public override List<string> getLableData() {
            List<string> list = new List<string>();
            list.Add("编号:" + src + "->" + des);
            list.Add("起点：x:" + sx + " y:" + sy + " z:" + sz);
            list.Add("终点：x:" + dx + " y:" + dy + " z:" + dz);
            list.Add(extra);
            return list;
        }

        public static TaskCrane GetRunTaskCrane(int stn) {
            return MyCache.getList<TaskCrane>().Where(x => x.status > 2 && x.status < 80 && x.des == stn).FirstOrDefault();
        }

        public override void init(CoreTask complete,CoreTaskParam param,TaskDto taskDto,List<TaskDto> keyList) {

            bussType = complete.bussType;

            extra = src + "-->" + des;
            changeSrc(Math.Abs(src));
            changeDes(Math.Abs(des));
            step = taskDto == keyList[keyList.Count - 1] ? 2 : 1;


        }

        public void changeSrc(int src) {
            this.src = src;
            CoreWhLoc EndPoint = CoreWhLoc.getCoreWhLoc(Math.Abs(src));
            if (EndPoint == null) {
                LoggerCommon.consoleOnly(0,"垛机未找到起点:  " + src + " not find");
                return;
            }

            craneId = EndPoint.craneId;
            driveId = craneId;
            sx = EndPoint.x;
            sy = EndPoint.y;
            sz = EndPoint.z;
        }

        public void changeDes(int des) {
            this.des = des;
            CoreWhLoc EndPoint = CoreWhLoc.getCoreWhLoc(Math.Abs(des));
            if (EndPoint == null) {
                LoggerCommon.consoleOnly(0,"垛机未找到位置:crane Pos " + des + " not find");
                return;
            }
            craneId = EndPoint.craneId;
            driveId = craneId;
            dx = EndPoint.x;
            dy = EndPoint.y;
            dz = EndPoint.z;
        }
    }
}
